﻿package com.cf.papervision3d.Utils.setting{
	
	import flash.events.MouseEvent;
	import org.papervision3d.cameras.Camera3D;
	public class viewMode extends Camera3D{
		public static const FOCUS:String = "FocusObject";
		public static const FREELOOK:String = "FreeLookByObject";
		public static const FIX:String = "FixPosition";
		public function set mode(_str:String):void{	if(_str==viewMode.FOCUS || _str==viewMode.FREELOOK || _str==viewMode.FIX) _mode=_str; }
		public function get mode():String{return _mode}
		private var _mode:String;
		private var _cameradistance:Number = -1000;
		private var _cameraPitch:Number = 90;
		private var _cameraYaw:Number = -90;
		private var _doRotation:Boolean; // let camera roll;
		private var _lastMouseX:int;
		private var _lastMouseY:int;
		public function viewMode():void{
			_mode = FIX;
			_doRotation = false;
			this.zoom = 10;
			this.focus = 50;
		}//fn
		
		/*public function focus(_obj:DisplayObject3D=null):void{
			camera.zoom = 10;
			camera.focus = 100;
			camera.y = _cameradistance;
			camera.orbit(_cameraPitch,_cameraYaw,true,_obj);
		}//fn*/
		
		public function onView(e:MouseEvent=null):void{
			switch(mode){
				default: /* do nothing */ break;
				case FOCUS:
					_lastMouseX = e.stageX;
					_lastMouseY = e.stageY;
					_doRotation = true;
					break;
				case FREELOOK:
					break;
			}//case
		}//fn
		public function onStop(e:MouseEvent=null):void {
			_doRotation = false;
		}//fn
		public function onZoom(e:MouseEvent=null):void{
			switch(mode){
				default: /* do nothing */ break;
				case FOCUS:
					adjustzoom(e.delta);
					break;
				case FREELOOK:
					break;
			}//case
		}//fn
		public function refresh(_mx:int, _my:int):void{
			// If the mouse button has been clicked then update the camera position
			if (_doRotation) {
				// convert the change in mouse position into a change in camera angle
				var _dPitch:Number = (_my - _lastMouseY) / 2;
				var _dYaw:Number = (_mx - _lastMouseX) / 2;
				// update the camera angles
				_cameraPitch -= _dPitch;
				_cameraYaw -= _dYaw;
				// limit the pitch of the camera
				if (_cameraPitch <= 0) {
					_cameraPitch = 0.1;
				} else if (_cameraPitch >= 180) {
					_cameraPitch = 179.9;
				}//if
				// reset the last mouse position
				_lastMouseX = _mx;
				_lastMouseY = _my;
				// reposition the camera
				this.orbit(_cameraPitch, _cameraYaw);
			}//if
		}//fn
		// Handler Camera
		private function adjustzoom(delta:int):void{
			this.zoom = Math.min(21, Math.max(2, this.zoom + delta / 3));
			//trace("Zoom Level = "+ this.zoom);
		}//fn
	}//class
}//package